package demo2;

public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        Thread t2 = new Thread(()-> {
            while (!Thread.currentThread().isInterrupted()){
                System.out.println("线程执行中");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //1.默认，清除标记， 忽略掉终止的信号
                    e.printStackTrace();
                    //2. 设置break，表示收到终止信号了就立马结束线程
                    //break;
                    //3.留给程序员决定，做了其他工作之后在结束
                    //这里做其他收尾的工作
                    break;
                }
            }
            System.out.println("线程终止");
        });
        t2.start();
        System.out.println("线程开始运行");
        System.out.println("5秒后线程停止");
        Thread.sleep(5000);
        System.out.println("设置标志位，让线程停止运行");
        t2.interrupt();
    }
}
